﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    class Monster1
    {
        private List<Point> Routes = new List<Point>{

            new Point(1,11),
            new Point(2,11),
            new Point(2,6),
            new Point(1,6),
            new Point(1,1),
            new Point(5,1),
            new Point(5,4),
            new Point(8,4),
            new Point(8,2),
            new Point(10,2),
            new Point(10,17),
            new Point(8,17),
            new Point(8,15),
            new Point(3,15),
            new Point(3,18),
            new Point(1,18),
            new Point(1,15)
        };//幽灵移动路线的目标点
        //定义怪物当前目标点
        private int CurrentDestnation { get; set; }
        //怪物当前坐标
        public Point Location { get; set; }
        //下次移动时间
        private DateTime NextMoveTime { get; set; }
        //初始化monster1
        public Monster1(int currentDestnation)
        {
            CurrentDestnation = currentDestnation;
            Location = Routes[currentDestnation];
            NextMoveTime = System.DateTime.Now.AddMilliseconds(200);
        }
        //坐标移动速度
        private Point speed
        {
            get
            {
                int PriorPoint = CurrentDestnation == 0 ? Routes.Count - 1 : CurrentDestnation - 1;
                int dx = Routes[CurrentDestnation].X - Routes[PriorPoint].X;//x移动速度
                int dy = Routes[CurrentDestnation].Y - Routes[PriorPoint].Y;//y移动速度
                return new Point(dx == 0 ? 0 : dx / Math.Abs(dx), dy == 0 ? 0 : dy / Math.Abs(dy));//返回怪物新坐标
            }
        }
        //移动函数
        public Point? Move()
        {
            //没到下次移动时间什么都不做
            if (NextMoveTime > System.DateTime.Now)
            {
                return null;
            }
            //到达当前的目标点
            if (Location == Routes[CurrentDestnation])
            {
                //get the goal,move to next
                CurrentDestnation += 1;
                if (CurrentDestnation >= Routes.Count)//到最后一个点目标点回归0
                {
                    CurrentDestnation = 0;
                }
            }
            else
            {
                Location = new Point(Location.X + speed.X, Location.Y + speed.Y);
            }
            NextMoveTime = System.DateTime.Now.AddMilliseconds(200);
            return Location;
        }
    }
}
